package com.jerei.utils;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;

import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * Created by sunfeihu on 2016/11/25.
 */
public class UtilsBean {

    public static <T> T wrapper(Class<T> clzz, Map<String, String[]> paramsMap) {
        T entity = null;
        try {
            entity = clzz.newInstance();
            BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(entity);
            beanWrapper.setAutoGrowNestedPaths(true);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            beanWrapper.registerCustomEditor(java.util.Date.class, new CustomDateEditor(df, true));
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            beanWrapper.registerCustomEditor(java.util.Date.class, new CustomDateEditor(df, true));
            beanWrapper.registerCustomEditor(java.lang.Boolean.class, new JrCustomBooleanEditor(true));
            String[] _value = null;
            if (null != paramsMap && paramsMap.size() > 0) {
                for (String key : paramsMap.keySet()) {
                    _value = paramsMap.get(key);
                    try {
                        if (null != _value && _value.length == 1) {
                            beanWrapper.setPropertyValue(key, _value[0]);
                        }
                    } catch (NotWritablePropertyException e) {
                        //如果属性不匹配
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }
}

class JrCustomBooleanEditor extends CustomBooleanEditor {

    public JrCustomBooleanEditor(boolean allowEmpty) {
        super(allowEmpty);
    }

    public JrCustomBooleanEditor(String trueString, String falseString, boolean allowEmpty) {
        super(trueString, falseString, allowEmpty);
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (null == text && "".equals(text)) {
            text = "false";
        }
        super.setAsText(text);
    }

    @Override
    public String getAsText() {
        String text = super.getAsText();
        if (null == text && "".equals(text)) {
            text = "false";
        }
        return text;
    }

    @Override
    public Object getValue() {
        Object value = super.getValue();
        if (null != value && value instanceof Boolean) {
            Boolean _v = (Boolean) value;
            if (_v) {
                return _v;
            } else {
                return Boolean.FALSE;
            }
        }
        return Boolean.FALSE;
    }

    @Override
    public void setValue(Object value) {
        if (null != value && value instanceof Boolean) {
            Boolean _v = (Boolean) value;
            if (_v) {
                super.setValue(Boolean.TRUE);
            } else {
                super.setValue(Boolean.FALSE);
            }
        } else {
            super.setValue(Boolean.FALSE);
        }
    }
}
